package com.eqdd.yiqidian.ui.tongxunlu.tag;

import android.databinding.DataBindingUtil;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;

import com.eqdd.yiqidian.AddTagActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.FriendListResultBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.utils.ItemDecorationUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.eqdd.yiqidian.widget.NewFlowLayoutManager;
import com.javonlee.dragpointview.OnPointDragListener;
import com.javonlee.dragpointview.view.AbsDragPointView;
import com.javonlee.dragpointview.view.DragPointView;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

public class AddTagActivity extends BaseActivity {
    private AddTagActivityCustom dataBinding;
    private SlimAdapterEx<FriendListResultBean> slimAdapterEx;
    private SlimAdapterEx<FriendListResultBean> slimAdapterExTag;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_add_tag);
        initTopTitleBar(View.VISIBLE, "添加新标签");
        initTopRightText("创建", v -> {
            if (!TextUtils.isEmpty(dataBinding.etTagName.getText())&&!TextUtils.isEmpty(getLableds())) {
                OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.ADD_TAG)
                        .params("owner", user.getGuid())
                        .params("labelName", dataBinding.etTagName.getText().toString())
                        .params("labelfriends", getLableds())
                        .execute(new DialogCallBack<HttpResult>(AddTagActivity.this) {
                            @Override
                            public void onSuccess(Response<HttpResult> response) {
                                HttpResult httpResult = response.body();
                                ToastUtil.showShort(httpResult.getMsg());
                                if (httpResult.getStatus() == 200) {
                                    setResult(Config.SUCCESS);
                                    finish();
                                }
                            }
                        });
            }else {
                ToastUtil.showShort("请保证标签名字唯一且至少选择一人");
            }
        });

    }

    private String getLableds() {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0, lenth = slimAdapterExTag.getData().size(); i < lenth; i++) {
            stringBuilder.append(slimAdapterExTag.getDataItem(i).getGuid()).append(";");
        }
        if (TextUtils.isEmpty(stringBuilder)) {
            return "";
        } else {
            return stringBuilder.toString();
        }
    }

    @Override
    public void initData() {

        dataBinding.recyclerViewTag.setLayoutManager(new NewFlowLayoutManager());
        slimAdapterExTag = SlimAdapterEx.create().register(R.layout.list_item_46, new SlimInjector<FriendListResultBean>() {
            @Override
            public void onInject(FriendListResultBean data, IViewInjector injector) {
                injector.text(R.id.dpv_name, data.getUpname())
                        .visible(R.id.dpv_name)
                        .with(R.id.dpv_name, view -> {
                            DragPointView dragPointView = (DragPointView) view;
                            dragPointView.setOnPointDragListener(new OnPointDragListener() {
                                @Override
                                public void onRemoveStart(AbsDragPointView absDragPointView) {

                                }

                                @Override
                                public void onRemoveEnd(AbsDragPointView absDragPointView) {
                                    int removePos = slimAdapterExTag.getData().indexOf(data);
                                    data.setCheck(false);
                                    slimAdapterEx.notifyItemChanged(slimAdapterEx.getData().indexOf(data));
                                    slimAdapterExTag.getData().remove(removePos);
                                    slimAdapterExTag.notifyItemRemoved(removePos);


                                }

                                @Override
                                public void onRecovery(AbsDragPointView absDragPointView) {

                                }
                            });
                        });
            }
        }).attachTo(dataBinding.recyclerViewTag).updateData(new ArrayList());
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_47, new SlimInjector<FriendListResultBean>() {
            @Override
            public void onInject(FriendListResultBean data, IViewInjector injector) {
                injector.enable(R.id.checkbox, data.isEnable())
                        .checked(R.id.checkbox, data.isCheck())
                        .imageCircle(R.id.iv_head, data.getIphoto())
                        .text(R.id.tv_content, data.getUpname());
            }
        }).attachTo(dataBinding.recyclerView).updateData(new ArrayList());
        ItemClickSupport.addTo(dataBinding.recyclerView)
                .setOnItemClickListener((recyclerView, position, v) -> {
                    if (slimAdapterEx.getDataItem(position).isEnable()) {
                        slimAdapterEx.getDataItem(position).setCheck(!slimAdapterEx.getDataItem(position).isCheck());
                        slimAdapterEx.notifyItemChanged(position);
                        if (slimAdapterEx.getDataItem(position).isCheck()) {
                            slimAdapterExTag.getData().add(slimAdapterEx.getDataItem(position));
                            slimAdapterExTag.notifyItemInserted(slimAdapterExTag.getData().size() - 1);
                        } else {
                            int removePos = slimAdapterExTag.getData().indexOf(slimAdapterEx.getDataItem(position));
                            slimAdapterExTag.getData().remove(removePos);
                            slimAdapterExTag.notifyItemRemoved(removePos);
                        }
                    }
                });


    }

    @Override
    public void setView() {
        OkGo.<HttpResult<List<FriendListResultBean>>>post(HttpConfig.BASE_URL + HttpConfig.FRIEND_LIST_INFO)
                .params("userGuid", user.getGuid())
                .execute(new DialogCallBack<HttpResult<List<FriendListResultBean>>>(this) {
                    @Override
                    public void onSuccess(Response<HttpResult<List<FriendListResultBean>>> response) {
                        if (response.body().getStatus() == 200) {
                            ItemDecorationUtil.openFloatDeco(AddTagActivity.this, dataBinding.recyclerView, response.body().getItems(), "upname");
                            slimAdapterEx.updateData(response.body().getItems());

                        }
                    }
                });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }
}
